home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / xdr / xdr.h < prev   
C/C++ Source or Header  |  1997-07-22  |  9KB  |  272 lines

  1. /* @(#)xdr.h    2.2 88/07/29 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. /*      @(#)xdr.h 1.19 87/04/22 SMI      */
  31.  
  32. /*
  33.  * xdr.h, External Data Representation Serialization Routines.
  34.  *
  35.  * Copyright (C) 1984, Sun Microsystems, Inc.
  36.  */
  37.  
  38. #ifndef __XDR_HEADER__
  39. #define __XDR_HEADER__
  40.  
  41. /*
  42.  * XDR provides a conventional way for converting between C data
  43.  * types and an external bit-string representation.  Library supplied
  44.  * routines provide for the conversion on built-in C data types.  These
  45.  * routines and utility routines defined here are used to help implement
  46.  * a type encode/decode routine for each user-defined type.
  47.  *
  48.  * Each data type provides a single procedure which takes two arguments:
  49.  *
  50.  *    bool_t
  51.  *    xdrproc(xdrs, argresp)
  52.  *        XDR *xdrs;
  53.  *        <type> *argresp;
  54.  *
  55.  * xdrs is an instance of a XDR handle, to which or from which the data
  56.  * type is to be converted.  argresp is a pointer to the structure to be
  57.  * converted.  The XDR handle contains an operation field which indicates
  58.  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
  59.  *
  60.  * XDR_DECODE may allocate space if the pointer argresp is null.  This
  61.  * data can be freed with the XDR_FREE operation.
  62.  *
  63.  * We write only one procedure per data type to make it easy
  64.  * to keep the encode and decode procedures for a data type consistent.
  65.  * In many cases the same code performs all operations on a user defined type,
  66.  * because all the hard work is done in the component type routines.
  67.  * decode as a series of calls on the nested data types.
  68.  */
  69.  
  70. /*
  71.  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
  72.  * stream.  XDR_DECODE causes the type to be extracted from the stream.
  73.  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
  74.  * request.
  75.  */
  76.  
  77. enum xdr_op {
  78.     XDR_ENCODE=0,
  79.     XDR_DECODE=1,
  80.     XDR_FREE=2 };
  81.  
  82. /*
  83.  * This is the number of bytes per unit of external data.
  84.  */
  85.  
  86. #define BYTES_PER_XDR_UNIT    (4)
  87. #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
  88.             * BYTES_PER_XDR_UNIT)
  89.  
  90. /*
  91.  * A xdrproc_t exists for each data type which is to be encoded or decoded.
  92.  *
  93.  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
  94.  * The opaque pointer generally points to a structure of the data type
  95.  * to be decoded.  If this pointer is 0, then the type routines should
  96.  * allocate dynamic storage of the appropriate size and return it.
  97.  * bool_t    (*xdrproc_t)(XDR *, caddr_t *);
  98.  */
  99. typedef    bool_t (*xdrproc_t)();
  100.  
  101. /*
  102.  * The XDR handle.
  103.  * Contains operation which is being applied to the stream,
  104.  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
  105.  * and two private fields for the use of the particular impelementation.
  106.  */
  107. typedef struct {
  108.     enum xdr_op    x_op;        /* operation; fast additional param */
  109.     struct xdr_ops {
  110.         bool_t    (*x_getlong)();    /* get a long from underlying stream */
  111.         bool_t    (*x_putlong)();    /* put a long to " */
  112.         bool_t    (*x_getbytes)();/* get some bytes from " */
  113.         bool_t    (*x_putbytes)();/* put some bytes to " */
  114.         u_int    (*x_getpostn)();/* returns bytes off from beginning */
  115.         bool_t  (*x_setpostn)();/* lets you reposition the stream */
  116.         long *    (*x_inline)();    /* buf quick ptr to buffered data */
  117.         void    (*x_destroy)();    /* free privates of this xdr_stream */
  118.     } *x_ops;
  119.     caddr_t     x_public;    /* users' data */
  120.     caddr_t        x_private;    /* pointer to private data */
  121.     caddr_t     x_base;        /* private used for position info */
  122.     int        x_handy;    /* extra private word */
  123. } XDR;
  124.  
  125. /*
  126.  * Operations defined on a XDR handle
  127.  *
  128.  * XDR        *xdrs;
  129.  * long        *longp;
  130.  * caddr_t     addr;
  131.  * u_int     len;
  132.  * u_int     pos;
  133.  */
  134. #define XDR_GETLONG(xdrs, longp)            \
  135.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  136. #define xdr_getlong(xdrs, longp)            \
  137.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  138.  
  139. #define XDR_PUTLONG(xdrs, longp)            \
  140.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  141. #define xdr_putlong(xdrs, longp)            \
  142.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  143.  
  144. #define XDR_GETBYTES(xdrs, addr, len)            \
  145.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  146. #define xdr_getbytes(xdrs, addr, len)            \
  147.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  148.  
  149. #define XDR_PUTBYTES(xdrs, addr, len)            \
  150.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  151. #define xdr_putbytes(xdrs, addr, len)            \
  152.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  153.  
  154. #define XDR_GETPOS(xdrs)                \
  155.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  156. #define xdr_getpos(xdrs)                \
  157.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  158.  
  159. #define XDR_SETPOS(xdrs, pos)                \
  160.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  161. #define xdr_setpos(xdrs, pos)                \
  162.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  163.  
  164. #define    XDR_INLINE(xdrs, len)                \
  165.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  166. #define    xdr_inline(xdrs, len)                \
  167.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  168.  
  169. #define    XDR_DESTROY(xdrs)                \
  170.     if ((xdrs)->x_ops->x_destroy)             \
  171.         (*(xdrs)->x_ops->x_destroy)(xdrs)
  172. #define    xdr_destroy(xdrs)                \
  173.     if ((xdrs)->x_ops->x_destroy)             \
  174.         (*(xdrs)->x_ops->x_destroy)(xdrs)
  175.  
  176. /*
  177.  * Support struct for discriminated unions.
  178.  * You create an array of xdrdiscrim structures, terminated with
  179.  * a entry with a null procedure pointer.  The xdr_union routine gets
  180.  * the discriminant value and then searches the array of structures
  181.  * for a matching value.  If a match is found the associated xdr routine
  182.  * is called to handle that part of the union.  If there is
  183.  * no match, then a default routine may be called.
  184.  * If there is no match and no default routine it is an error.
  185.  */
  186. #define NULL_xdrproc_t ((xdrproc_t)0)
  187. struct xdr_discrim {
  188.     int    value;
  189.     xdrproc_t proc;
  190. };
  191.  
  192. /*
  193.  * In-line routines for fast encode/decode of primitve data types.
  194.  * Caveat emptor: these use single memory cycles to get the
  195.  * data from the underlying buffer, and will fail to operate
  196.  * properly if the data is not aligned.  The standard way to use these
  197.  * is to say:
  198.  *    if ((buf = XDR_INLINE(xdrs, count)) == NULL)
  199.  *        return (FALSE);
  200.  *    <<< macro calls >>>
  201.  * where ``count'' is the number of bytes of data occupied
  202.  * by the primitive data types.
  203.  *
  204.  * N.B. and frozen for all time: each data type here uses 4 bytes
  205.  * of external representation.
  206.  */
  207. #define IXDR_GET_LONG(buf)        ((long)ntohl((u_long)*(buf)++))
  208. #define IXDR_PUT_LONG(buf, v)        (*(buf)++ = (long)htonl((u_long)v))
  209.  
  210. #define IXDR_GET_BOOL(buf)        ((bool_t)IXDR_GET_LONG(buf))
  211. #define IXDR_GET_ENUM(buf, t)        ((t)IXDR_GET_LONG(buf))
  212. #define IXDR_GET_U_LONG(buf)        ((u_long)IXDR_GET_LONG(buf))
  213. #define IXDR_GET_SHORT(buf)        ((short)IXDR_GET_LONG(buf))
  214. #define IXDR_GET_U_SHORT(buf)        ((u_short)IXDR_GET_LONG(buf))
  215.  
  216. #define IXDR_PUT_BOOL(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  217. #define IXDR_PUT_ENUM(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  218. #define IXDR_PUT_U_LONG(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  219. #define IXDR_PUT_SHORT(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  220. #define IXDR_PUT_U_SHORT(buf, v)    IXDR_PUT_LONG((buf), ((long)(v)))
  221.  
  222. /*
  223.  * These are the "generic" xdr routines.
  224.  */
  225. extern bool_t    xdr_void();
  226. extern bool_t    xdr_int();
  227. extern bool_t    xdr_u_int();
  228. extern bool_t    xdr_long();
  229. extern bool_t    xdr_u_long();
  230. extern bool_t    xdr_short();
  231. extern bool_t    xdr_u_short();
  232. extern bool_t    xdr_bool();
  233. extern bool_t    xdr_enum();
  234. extern bool_t    xdr_array();
  235. extern bool_t    xdr_bytes();
  236. extern bool_t    xdr_opaque();
  237. extern bool_t    xdr_string();
  238. extern bool_t    xdr_union();
  239. extern bool_t    xdr_char();
  240. extern bool_t    xdr_u_char();
  241. extern bool_t    xdr_vector();
  242. extern bool_t    xdr_float();
  243. extern bool_t    xdr_double();
  244. extern bool_t    xdr_reference();
  245. extern bool_t    xdr_pointer();
  246. extern bool_t    xdr_wrapstring();
  247.  
  248. /*
  249.  * Common opaque bytes objects used by many rpc protocols;
  250.  * declared here due to commonality.
  251.  */
  252. #define MAX_NETOBJ_SZ 1024 
  253. struct netobj {
  254.     u_int    n_len;
  255.     char    *n_bytes;
  256. };
  257. typedef struct netobj netobj;
  258. extern bool_t   xdr_netobj();
  259.  
  260. /*
  261.  * These are the public routines for the various implementations of
  262.  * xdr streams.
  263.  */
  264. extern void   xdrmem_create();        /* XDR using memory buffers */
  265. extern void   xdrstdio_create();    /* XDR using stdio library */
  266. extern void   xdrrec_create();        /* XDR pseudo records for tcp */
  267. extern bool_t xdrrec_endofrecord();    /* make end of xdr record */
  268. extern bool_t xdrrec_skiprecord();    /* move to beginning of next record */
  269. extern bool_t xdrrec_eof();        /* true if no more input */
  270.  
  271. #endif /* __XDR_HEADER__ */
  272.